//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _token: '',
    pagesType: '',//页面类型
    addressobj: {},//地址列表含分页
    addressList: [],//地址列表
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    console.info(option)
    var that = this;
    var _key = wx.getStorageSync("token");
    that.setData({
      _token: _key,
      pagesType: option.pagesType
    })
    wx.showLoading({
      title: '数据加载中',
    })
    //获取地址列表
    that.getAddressList(_key);
  },
  //获取地址列表
  getAddressList: function (token) {
    var that = this;
    wx.request({
      url: app.globalData.httpsPath + '/user/address/index',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: token,
      },
      //请求后台数据成功  
      success: function (res) {
        console.info(res);
        var _addressobj = res.data.data;
        var _addressList = res.data.data.data;
        console.info(_addressList)
        // 隐藏加载框
        wx.hideLoading();
        that.setData({
          addressobj: _addressobj,
          addressList: _addressList
        })
        if (_addressList.length == 1) {
          wx.request({
            url: app.globalData.httpsPath + '/user/address/setdefault',
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            data: {
              token: token,
              id: _addressList[0].id,
            },
            //请求后台数据成功  
            success: function (res) {
              if (res.data.status == true) {
                //that.getAddressList(token)
                console.info("设为默认成功")
              }
            }
          })
        }
      }
    })
  },
  //添加地址
  addNewAddress: function () {
    var that = this;
    var _pagetype = that.data.pagesType;
    wx.navigateTo({
      url: '../addNewaddress/addNewaddress?type=new&pagesType=' + _pagetype,
    })
  },
  //设为默认
  radioChangeDefault: function (e) {
    var that = this;
    var _id = e.detail.value;
    var _token = that.data._token;
    wx.request({
      url: app.globalData.httpsPath + '/user/address/setdefault',
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      data: {
        token: _token,
        id: _id,
      },
      //请求后台数据成功  
      success: function (res) {
        if (res.data.status == true) {
          that.getAddressList(_token)
        }
      }
    })
  },
  //修改地址
  editAddress: function (e) {
    var that = this;
    var _id = e.currentTarget.dataset.id;
    var _pagetype = that.data.pagesType;
    wx.navigateTo({
      url: '../addNewaddress/addNewaddress?type=edit&addreddId=' + _id + '&pagesType=' + _pagetype,
    })
  },
  //删除地址
  delAddress: function (e) {
    var that = this;
    var _token = that.data._token;
    var _id = e.currentTarget.dataset.id;//id

    wx.showModal({
      title: '删除地址',
      content: '确认删除当前地址吗？',
      success(res) {
        if (res.confirm) {

          wx.request({
            url: app.globalData.httpsPath + '/user/address/del',
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            data: {
              token: _token,
              id: _id
            },
            //请求后台数据成功  
            success: function (res) {
              //console.info(res)
              if (res.data.status == true) {
                //重新获取列表
                that.getAddressList(_token);
              }
            }
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //选择地址
  selectAddress: function (e) {
    
    var that = this;
    var _token = that.data._token;
    var _id = e.currentTarget.dataset.id;//id
    var _pagetype = that.data.pagesType;
    console.info(_pagetype)
    if (_pagetype == 'selected') {
      wx.request({
        url: app.globalData.httpsPath + '/user/address/info',
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        data: {
          token: wx.getStorageSync("token"),
          id: _id,
        },
        //请求后台数据成功  
        success: function (res) {
          console.info(res);
          if (res.data.status == true) {
            var addressInfo = res.data.data;

            if (addressInfo.province == null) {
              addressInfo.province = "";
            }
            if (addressInfo.city == null) {
              addressInfo.city = "";
            }
            if (addressInfo.zone == null) {
              addressInfo.zone = "";
            }
            //存储本地
            wx.setStorageSync('addressInfo', addressInfo)
            wx.navigateTo({
              url: '../confirmOrder/confirmOrder?pagesType=selected',
            })
          }
        }
      })
    }
  },

  
})